package ruifeng.zcj.dig.ui.forum;

import android.content.Context;
import android.content.Intent;
import android.text.ClipboardManager;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import com.easemob.easeui.domain.EaseEmojicon;
import com.easemob.easeui.ui.EaseBaiduMapActivity;
import com.easemob.easeui.widget.EaseChatExtendMenu;
import com.easemob.easeui.widget.EaseChatInputMenu;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

import org.greenrobot.eventbus.EventBus;

import ruifeng.zcj.dig.R;
import ruifeng.zcj.dig.base.MyApp;
import ruifeng.zcj.dig.base.api.OSChinaApi;
import ruifeng.zcj.dig.base.bean.Entity;
import ruifeng.zcj.dig.ui.forum.bean.NewPost;
import ruifeng.zcj.mybase.base.BaseFragment;
import ruifeng.zcj.mybase.base.utils2.AbJsonUtil;

/**
 * 发送内容按钮
 * Created by Administrator on 2016/10/13 0013.
 */
public class SendMessageFragment extends BaseFragment {
    protected EaseChatInputMenu inputMenu;
    static final int ITEM_TAKE_PICTURE = 1;
    static final int ITEM_PICTURE = 2;
    static final int ITEM_LOCATION = 3;
    protected static final int REQUEST_CODE_MAP = 1;
    protected int[] itemStrings = {R.string.attach_take_pic, R.string.attach_picture, R.string.attach_location};
    protected int[] itemdrawables = {R.drawable.ease_chat_takepic_selector, R.drawable.ease_chat_image_selector,
            R.drawable.ease_chat_location_selector};
    protected int[] itemIds = {ITEM_TAKE_PICTURE, ITEM_PICTURE, ITEM_LOCATION};
    protected MyItemClickListener extendMenuItemClickListener;
    protected InputMethodManager inputManager;
    protected ClipboardManager clipboard;

    protected ForumDetailActivity mForumDetailActivity;
    protected NewPost.RowsBean mRowsBean;

    MyApp myApps;


    @Override
    protected void initFragmentConfig(View view) {
        // inputMenu.hideExtendMenuContainer();  隐藏输入键盘
        mForumDetailActivity = (ForumDetailActivity) mActivity;
        myApps = (MyApp) mApp;

        inputMenu = (EaseChatInputMenu) getView().findViewById(R.id.input_menu);
        extendMenuItemClickListener = new MyItemClickListener();

        inputMenu.init(null);

        inputMenu.setChatInputMenuListener(new EaseChatInputMenu.ChatInputMenuListener() {
            @Override
            public void onSendMessage(String content) {
                // 发送文本消息
                sendTextMessage(content);
            }

            @Override
            public void onBigExpressionClicked(EaseEmojicon emojicon) {
                //发送大表情(动态表情)
                //   sendBigExpressionMessage(emojicon.getName(), emojicon.getIdentityCode());

            }

            @Override
            public boolean onPressToSpeakBtnTouch(View v, MotionEvent event) {//  发送语音

                return false;
            }
        });

        inputManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        clipboard = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

    }

    /****
     * 发送文本
     ***/
    private void sendTextMessage(final String content) {
        if (mRowsBean != null) {
            if (TextUtils.isEmpty(myApps.getUserId())) {
                myApps.setUserId("6");
            }
            OSChinaApi.sendcomment(mRowsBean.getPostingsid(),myApps.getUserId(), content, coomType(), new RequestCallBack<String>() {
                @Override
                public void onSuccess(ResponseInfo<String> responseInfo) {
                    if (!TextUtils.isEmpty(responseInfo.result)) {
                        Entity entity = (Entity) AbJsonUtil.fromJson(responseInfo.result, Entity.class);
                        if (entity.OK()) {
                            EventBus.getDefault().post(content);
                            showSnackBar("评论成功");
                        } else {
                            showSnackBar("评论失败");
                        }
                    }
                }

                @Override
                public void onFailure(HttpException e, String s) {
                    showSnackBar("评论失败");
                }
            });
        }
    }

    /***
     * 判断是否是：1贴子评论 2招聘求职评论
     **/
    protected String coomType() {
        return "1";
    }

    /**
     * 注册底部菜单扩展栏item; 覆盖此方法时如果不覆盖已有item，item的id需大于3
     */
    protected void registerExtendMenuItem() {
        for (int i = 0; i < itemStrings.length; i++) {
            inputMenu.registerExtendMenuItem(itemStrings[i], itemdrawables[i], itemIds[i], extendMenuItemClickListener);
        }
    }

    @Override
    protected int getContentView() {
        return R.layout.fragment_send_input_menu;
    }

    @Override
    protected void onFirstUserVisible() {
        mRowsBean = mForumDetailActivity.getRowsBean();

    }

    @Override
    protected void onUserVisible() {

    }

    @Override
    protected void onUserInvisible() {

    }

    @Override
    protected void cleanMemory() {

    }

    /**
     * 扩展菜单栏item点击事件
     */
    class MyItemClickListener implements EaseChatExtendMenu.EaseChatExtendMenuItemClickListener {

        @Override
        public void onClick(int itemId, View view) {
            switch (itemId) {
                case ITEM_TAKE_PICTURE: // 拍照
                    // selectPicFromCamera();
                    break;
                case ITEM_PICTURE:
                    //   selectPicFromLocal(); // 图库选择图片
                    break;
                case ITEM_LOCATION: // 位置
                    startActivityForResult(new Intent(getActivity(), EaseBaiduMapActivity.class), REQUEST_CODE_MAP);
                    break;

                default:
                    break;
            }
        }

    }

}
